home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / folder.tcl.z / folder.tcl
Text File  |  2002-07-08  |  9KB  |  360 lines

  1. # folder.tcl
  2. #
  3. # Folder operations, minus scan & inc.
  4. #
  5. # Copyright (c) 1993 Xerox Corporation.
  6. # Use and copying of this software and preparation of derivative works based
  7. # upon this software are permitted. Any distribution of this software or
  8. # derivative works must comply with all applicable United States export
  9. # control laws. This software is made available AS IS, and Xerox Corporation
  10. # makes no warranty about the software, its performance or its conformity to
  11. # any specification.
  12.  
  13. proc Folder_Init {} {
  14.     global exmh argc argv mhProfile
  15.     set exmh(target) {}        ;# Name of target, for refile
  16.     set exmh(started) 0        ;# For Folder_Change, the first time
  17.     if {$argc > 0 && \
  18.     [file isdirectory $mhProfile(path)/[lindex $argv 0]]} then {
  19.     #scan named folder
  20.     set exmh(folder) $argv
  21.     } else {
  22.     if [catch {exec folder -fast < /dev/null} f] {
  23.         set exmh(folder) {}
  24.     } else {
  25.         set exmh(folder) $f
  26.     }
  27.     }
  28. }
  29.  
  30. proc Folder_Summary { folder } {
  31.     global mhProfile env
  32.     if [catch {pwd} cwd] {
  33.     Exmh_Status $cwd
  34.     cd
  35.     set cwd [pwd]
  36.     }
  37.     if [catch {cd $mhProfile(path)/$folder}] {
  38.     catch {cd $cwd}
  39.     return "${folder}+ does not exist"
  40.     }
  41.     set low 100000
  42.     set high 0
  43.     set num 0
  44.     if {[catch {glob *} files] == 0} {
  45.     foreach f $files {
  46.         if {[regexp {^[0-9]+$} $f]} {
  47.         if {$f < $low} {
  48.             set low $f
  49.         }
  50.         if {$f > $high} {
  51.             set high $f
  52.         }
  53.         incr num
  54.         }
  55.     }
  56.     }
  57.     catch {cd $cwd}
  58.     if {$num <= 0} {
  59.     return "${folder}+ has no messages"
  60.     } else {
  61.     return "${folder}+ $num msgs ($low-$high)"
  62.     }
  63. }
  64.  
  65. proc Folder_Change {f {msgShowProc Msg_ShowCurrent}} {
  66. #    LogStart "Folder_Change $f"
  67.     Exmh_Debug ****************
  68.     Exmh_Debug Folder_Change $f [time [list  FolderChange $f $msgShowProc]]
  69. }
  70. proc FolderChange {f msgShowProc} {
  71.     global exmh mhProfile ftoc
  72.     if {[string compare [wm state .] normal] != 0} {
  73.     if {$exmh(iconic)} {
  74.         # Ignore once if starting up with -iconic flag
  75.         set exmh(iconic) 0
  76.     } else {
  77.         wm deiconify .
  78.     }
  79.     }
  80.     if {$exmh(started) && [Ftoc_Changes "Change folder"] > 0} {
  81.     # Need to reselect previous button here
  82.     return
  83.     }
  84.     # Trim off leading mail path
  85.     if [regsub ^$mhProfile(path)/ $f {} newf] {
  86.     set f $newf
  87.     }
  88.     if {[string length $f] == 0} {
  89.     return
  90.     }
  91.     if ![file isdirectory $mhProfile(path)/$f] {
  92.     Exmh_Status "Folder $f doesn't exist" purple
  93.     return
  94.     }
  95.     set oldFolder $exmh(folder)
  96.     Exmh_Status "Changing to $f ..."
  97.     if {$f != $exmh(folder)} {
  98.     Exmh_Debug Exmh_CheckPoint [time Exmh_CheckPoint]
  99.     global mhProfile
  100.     set summary [Mh_Folder $f]    ;# Set MH folder state
  101.     } else {
  102.     if {$ftoc(folder) == {} && $exmh(started)} {
  103.         # pseudo-display -> Checkpoint to set cur msg
  104.         # startup -> don't checkpoint (clears cur sequence)
  105.         Exmh_Debug Exmh_CheckPoint [time Exmh_CheckPoint]
  106.         }
  107.     set summary {}
  108.     }
  109.     set exmh(started) 1
  110.     global folderHook
  111.     if [info exists folderHook(leave,$oldFolder)] {
  112.     $folderHook(leave,$oldFolder) $oldFolder leave
  113.     }
  114.     Label_Folder $f $summary
  115.     Fdisp_HighlightCur $f
  116.     Flist_Visited $f
  117.     set exmh(folder) $f
  118.     if {$ftoc(autoSort)} {
  119.     if [Flist_NumUnseen $f] {
  120.         Ftoc_Sort
  121.     }
  122.     }
  123.     Scan_Folder $f 1
  124.     Exmh_Status $f
  125.     # Either Msg_ShowCurrent or Msg_ShowUnseen
  126.     eval $msgShowProc
  127.  
  128.     # Take any required folder-specific action (e.g., for drafts folder)
  129.     if [info exists folderHook(enter,$f)] {
  130.     $folderHook(enter,$f) $f enter
  131.     }
  132.     foreach cmd [info commands Hook_FolderChange*] {
  133.     $cmd $f
  134.     }
  135. }
  136.  
  137. proc Folder_Unseen {} {
  138.     Ftoc_NextFolder
  139. #    Folder_Change [Flist_NextUnseen]
  140. }
  141.  
  142. proc Folder_Target {f} {
  143.     global exmh mhProfile
  144.  
  145.     if ![file isdirectory $mhProfile(path)/$f] {
  146.     Exmh_Status "$mhProfile(path)/$f doesn't exist"
  147.     return 0
  148.     }
  149.     if {$exmh(folder) == $f} {
  150.     Exmh_Status "Target must be different than current" red
  151.     return 0
  152.     }
  153.     Fdisp_HighlightTarget $f
  154.     set exmh(target) $f
  155.     Exmh_Status "$f is target for moves and copies"
  156.     return 1
  157. }
  158. proc Folder_TargetMove { f {moveProc Ftoc_MoveMark} } {
  159.     if [Folder_Target $f] {
  160.     Msg_Move $moveProc
  161.     Fcache_Folder $f
  162.     }
  163. }
  164.  
  165. proc Folder_TargetCopy { f {copyProc Ftoc_CopyMark} } {
  166.     if [Folder_Target $f] {
  167.     Msg_Move $copyProc advance?
  168.     Fcache_Folder $f
  169.     }
  170. }
  171.  
  172. proc Folder_TargetClear {} {
  173.     global exmh
  174.  
  175.     Fdisp_HighlightTarget ""
  176.     set exmh(target) ""
  177.     Exmh_Status "No target set for moves and copies"
  178. }
  179.  
  180.  
  181. proc Folder_Sort { args } {
  182.     global exmh
  183.  
  184.     if {[Ftoc_Changes "Sort"] == 0} then {
  185.     Background_Wait
  186.     Exmh_Status "Sorting folder..." blue
  187.     eval {Mh_Sort $exmh(folder)} $args
  188.      Flist_ResetUnseen $exmh(folder)     ;# unseen sequence might have changed
  189.     Scan_FolderForce
  190.     set id [Mh_Cur $exmh(folder)]
  191.     if {$id != {}} {
  192.         Msg_Change $id
  193.     } else {
  194.         Msg_ClearCurrent
  195.     }
  196.     }
  197. }
  198.  
  199. proc Folder_Previous {} {
  200.     set f [Ftoc_LastFolder]
  201.     if {[string length $f]} {
  202.     Folder_Change $f
  203.     }
  204. }
  205.  
  206. proc Folder_Pack {} {
  207.     global exmh
  208.  
  209.     if {[Ftoc_Changes "Pack"] == 0} then {
  210.     Background_Wait
  211.     Exmh_Status "Packing folder..." blue
  212.     Mh_Pack $exmh(folder)
  213.      Flist_ResetUnseen $exmh(folder)     ;# unseen sequence might have changed
  214.     Scan_FolderForce
  215.     set id [Mh_Cur $exmh(folder)]
  216.     if {$id != {}} {
  217.         Msg_Change $id
  218.     } else {
  219.         Msg_ClearCurrent
  220.     }
  221.     }
  222. }
  223. proc Folder_Commit { {rmmCommit Mh_Rmm} {moveCommit Mh_Refile} {copyCommit Mh_Copy} } {
  224.     busy FolderCommit $rmmCommit $moveCommit $copyCommit
  225.     return 0
  226. }
  227. proc FolderCommit { rmmCommit moveCommit copyCommit } {
  228.     global exmh exwin ftoc
  229.  
  230.     Msg_CheckPoint    ;# Update sequence state
  231.     Ftoc_Commit $rmmCommit $moveCommit $copyCommit
  232.     Exmh_Debug Scan_CacheUpdate [time Scan_CacheUpdate]
  233.  
  234.     if $ftoc(autoPack) {
  235.     Background_Wait    ;# Let folder ops complete
  236.         Folder_Pack    ;# Before packing
  237.     }
  238.     Label_Folder $exmh(folder)
  239. }
  240. # Streamlined Commit called before Folder_Change
  241. proc Folder_CommitType { type } {
  242.     global ftoc exmh
  243.     Exmh_Debug Folder_CommitType $type
  244.     if {[string compare $type "Change folder"] == 0} {
  245.     Msg_CheckPoint
  246.     busy Ftoc_Commit Mh_Rmm Mh_Refile Mh_Copy
  247.     if $ftoc(autoPack) {
  248.         Background_Wait    ;# Let folder ops complete
  249.         Folder_Pack        ;# Before packing
  250.     }
  251.     } else {
  252.     Folder_Commit
  253.     }
  254. }
  255.  
  256. proc Folder_Purge { {folder {}} } {
  257.     global exmh
  258.     if {[string length $folder] == 0} {
  259.     set folder $exmh(folder)
  260.     }
  261.     set uid 0
  262.     while {[file exists [set fn /tmp/exmh.[pid].touch.$uid]]} {
  263.     incr uid
  264.     }
  265.     exec touch $fn
  266.     set now [file mtime $fn]
  267.     File_Delete $fn
  268.  
  269.     global mhProfile
  270.     if ![info exists mhProfile(delprefix)] {
  271.     set mhProfile(delprefix) #
  272.     }
  273.     if ![info exists mhProfile(purgeage)] {
  274.     set mhProfile(purgeage) 7
  275.     }
  276.     set purgesecs [expr $mhProfile(purgeage) * 24 * 60 * 60]
  277.     set n 0
  278.     foreach f [glob -nocomplain $mhProfile(path)/$folder/$mhProfile(delprefix)*] {
  279.     if {[file mtime $f] + $purgesecs < $now} {
  280.         Exmh_Debug Purge $f
  281.         File_Delete $f
  282.         incr n
  283.     }
  284.     }
  285.     if {$n > 0} {
  286.     Exmh_Status "Folder_Purge $folder $n msgs purged"
  287.     }
  288.     return $n
  289. }
  290.  
  291. proc Folder_PurgeAll {} {
  292.     global flist
  293.     set n 0
  294.     foreach f $flist(allfolders) {
  295.     incr n [Folder_Purge $f]
  296.     }
  297.     if {$n > 0} {
  298.     Exmh_Status "Folder_PurgeAll $n msgs purged total"
  299.     }
  300. }
  301.  
  302. proc Folder_PurgeBg { {folderlist {}} } {
  303.     global exmh mhProfile wish
  304.     if {[string length $folderlist] == 0} {
  305.     set folderlist $exmh(folder)
  306.     }
  307.     set uid 0
  308.     while {[file exists [set fn [Env_Tmp]/exmh.[pid].purge.$uid]]} {
  309.     incr uid
  310.     }
  311.     catch {File_Delete}    ;# auto-load it
  312.  
  313.     set out [open $fn w]
  314.     puts $out "wm withdraw ."
  315.     puts $out "source $exmh(library)/folder.tcl"
  316.     puts $out [list set mhProfile(delprefix) $mhProfile(delprefix)]
  317.     puts $out [list set mhProfile(purgeage) $mhProfile(purgeage)]
  318.     puts $out [list set mhProfile(path) $mhProfile(path)]
  319.     puts $out "proc Exmh_Status { s } \{catch \{send \"[winfo name .]\" \[list Exmh_Status \$s]\}\}"
  320.     puts $out "proc Exmh_Debug { args } {}"
  321.     puts $out [list proc File_Delete [info args File_Delete] [info body File_Delete]]
  322.     foreach folder $folderlist {
  323.     puts $out "Folder_Purge $folder"
  324.     }
  325.     puts $out "exec rm $fn"
  326.     puts $out exit
  327.     close $out
  328.     exec $wish -f $fn &
  329. }
  330. proc Folder_PurgeAllBg {} {
  331.     global flist
  332.     Folder_PurgeBg $flist(allfolders)
  333. }
  334.  
  335. # Called when changing messages.  If you are sharing a folder,
  336. # you need to checkpoint state to the file system at each message.
  337.  
  338. proc Folder_CheckPointShared {} {
  339.     global exmh folder
  340.     if {[info exist folder(shared,$exmh(folder))]} {
  341.     Msg_CheckPoint
  342.     }
  343. }
  344. proc Folder_IsShared {folder} {
  345.     global folder
  346.     set folder(shared,$folder) 1
  347. }
  348. proc Folder_FindShared {} {
  349.     global mhProfile
  350.     if {[catch {open $mhProfile(path)/.folders_shared} in]} {
  351.     Exmh_Debug $in
  352.     return
  353.     }
  354.     foreach f [split [read $in] \n] {
  355.     Foldler_IsShared $f
  356.     }
  357.     close $in
  358. }
  359.